// pages/contact/contact.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contact: [],
    array: ['7天', '14天', '21天', '28天'],
  },
  admin: function (res) {
    this.setData({
      bwadmin: res.detail.value
    })
  },
  bwtime: function (res) {
    console.log('picker发送选择改变，携带值为', res.detail.value)
    if (res.detail.value == 0) {
      this.setData({
        time: 7
      })
    }
    if (res.detail.value == 1) {
      this.setData({
        time: 14
      })
    }
    if (res.detail.value == 2) {
      this.setData({
        time: 21
      })
    }
    if (res.detail.value == 3) {
      this.setData({
        time: 28
      })
    }
  },

  userdata: function () {
    console.log(getApp().globalData.openid)
    const db = wx.cloud.database()
    db.collection("book_user")
      .where({
        _openid: getApp().globalData.openid
      })
      .get({
        success: res => {
            this.setData({
              userInfo: res.data[0]
            })
            if(res.data[0].name==null || res.data[0].sno==null ||res.data[0].grade==null || res.data[0].sex==null || res.data[0].contact==null || res.data[0].name=="" || res.data[0].sno=="" ||res.data[0].grade=="" || res.data[0].sex=="" || res.data[0].contact=="" ){
              wx.showModal({
                showCancel: false,
                content: '个人信息不全！',
                success(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.switchTab({
                      url: '../mine/mine',
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
        },
        fail: err => {
          console.log(err)
        }
      })

  },

  insertbookdate: function () {
  
    if (this.data.time == null) {
      wx.showToast({
        icon: 'none',
        title: '未选择借阅时长',
        duration: 1000
      })
    }else if (this.data.bwadmin == null) {
        wx.showToast({
          icon: 'none',
          title: '未填写管理员姓名',
          duration: 1000
        })
      } else {
          //获取当前时间
    var timestamp = Date.parse(new Date());
    var timestamp = timestamp / 1000;
    var n = timestamp * 1000;
    var date = new Date(n);
    var Y = date.getFullYear();  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);   
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
    var h = date.getHours();  
    var m = date.getMinutes();  
    var s = date.getSeconds();  
    console.log("当前时间：" ,Y+"/"+M+"/"+D+" "+h+":"+m+":"+s);  
    //添加数据
    const db = wx.cloud.database()
    const _ = db.command
    db.collection("book_list")
      .doc(this.data.id)
      .update({
        data: {
          borrow_info: _.push({
            each: [{
              borrow_openid: getApp().globalData.openid,
              borrow_user: this.data.userInfo.name,
              borrow_user_contact: this.data.userInfo.contact,
              borrow_bwadmin: this.data.bwadmin,
              borrow_bwtime: this.data.time,
              borrow_date: Y+"/"+M+"/"+D+" "+h+":"+m+":"+s,
              borrow_timestamp: timestamp,
              return_date: ''
            }],
          })
        },
        success: function () {
          console.log(getApp().globalData.openid);
          //console.log(this.data.userInfo.name);  
          wx.showToast({
            title: '借阅成功',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          });
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  },

  booksum: function () {
    const db = wx.cloud.database()
    db.collection("book_user")
      .where({
        _openid: getApp().globalData.openid
      })
      .get().then(res => {
        console.log('6666', res.data[0].booksum)
        db.collection("book_user")
          .where({
            _openid: getApp().globalData.openid
          })
          .update({
            data: {
              booksum: res.data[0].booksum + 1
            },
          })
      })
  },



  bw: function () {
    const db = wx.cloud.database()
    db.collection("book_list")
      .doc(this.data.id)
      .update({
        data: {
          borrow: '已经被借阅'
        },
      })
    this.insertbookdate()
    this.booksum()
  },



  onGetOpenid: function () {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //获取用户id
    this.onGetOpenid()
    //获取用户信息
    this.userdata()
    //获取图书信息
    console.log('43234', getApp().globalData.userInfo)
    const db = wx.cloud.database()
    db.collection("book_list")
      .doc(options.id)
      .get({
        success: res => {
          console.log(res.data),
            this.setData({
              contact: res.data,
              id: options.id
            })
          console.log('id', this.data.id)
        },
        fail: err => {
          console.log(err)
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})